<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <script>
    var minimum = function (s) {
      // 动态规划 非常非常巧妙
      let f = 0
      let cnt_b = 0
      for (let i = 0; i < s.length; i++) {
        if (s[i] == 'b')
          cnt_b++
        else {
          f = Math.min(f + 1, cnt_b)
        }
      }
      return f
    };
    console.log(minimum("aababbab"))
  </script>
</body>

</html>